<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html><head>
<!--Converted with LaTeX2HTML 98.1 release (February 19th, 1998)
originally by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->


<title>Box of Bricks</title>
<meta name="description" content="Box of Bricks">
<meta name="keywords" content="htmlatex">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="STYLESHEET" href="acm-00591_files/htmlatex.css">
<script type="text/javascript" src="acm-00591_files/jsapi"></script><script src="acm-00591_files/a" type="text/javascript"></script><script src="acm-00591_files/defaultes.js" type="text/javascript"></script></head><body bgcolor="#ffffff" lang="EN">

<h1><br clear="all"><center><table bgcolor="#0060f0"><tbody><tr><td><b><font color="#c0ffff" size="5">&nbsp;<a name="SECTION0001000000000000000000">
Box of Bricks</a>&nbsp;</font></b></td></tr></tbody></table></center>
</h1>

<p>
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds
stacks of different height. ``Look, I've built a wall!'', he tells his older sister Alice. ``Nah, you should
make all stacks the same height. Then you would have a real wall.'', she retorts. After a little con-
sideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all
stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum
number of bricks moved. Can you help?

</p><p>
</p><div align="center">
<img src="acm-00591_files/p591.gif">
</div>

<p>

</p><h2><font color="#0070e8"><a name="SECTION0001001000000000000000">
Input</a>&nbsp;</font>
</h2>
The input consists of several data sets. Each set begins with a line containing the number <i>n</i> of stacks
Bob has built. The next line contains <i>n</i> numbers, the heights <i>h</i><sub><i>i</i></sub> of the <i>n</i> stacks. You may assume

<!-- MATH: $1 &#376;\le n \le&#376; 50$ -->
<img src="acm-00591_files/591img1.gif" alt="$1 &#376;\le n \le&#376; 50$" align="middle" border="0" width="88" height="30">
and 
<!-- MATH: $1 \le&#376; h_i &#376;\le 100$ -->
<img src="acm-00591_files/591img2.gif" alt="$1 \le&#376; h_i &#376;\le 100$" align="middle" border="0" width="101" height="32">.

<p>
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible
to rearrange the bricks such that all stacks have the same height.

</p><p>
The input is terminated by a set starting with <i>n</i> = 0. This set should not be processed.

</p><p>

</p><h2><font color="#0070e8"><a name="SECTION0001002000000000000000">
Output</a>&nbsp;</font>
</h2>
For each set, first print the number of the set, as shown in the sample output. Then print the line
``<tt>The minimum number of moves is </tt><i>k</i>.'', where <i>k</i> is the minimum number of bricks that
have to be moved in order to make all the stacks the same height.

<p>
Output a blank line after each set.

</p><p>

</p><h2><font color="#0070e8"><a name="SECTION0001003000000000000000">
Sample Input</a>&nbsp;</font>
</h2>
<pre>6
5 2 4 1 7 5
0
</pre>

<p>

</p><h2><font color="#0070e8"><a name="SECTION0001004000000000000000">
Sample Output</a>&nbsp;</font>
</h2>
<pre>Set #1
The minimum number of moves is 5.
</pre>

<p>

</p><p>
<br></p><hr>
<address>
<i>Miguel A. Revilla</i>
<br><i>1998-03-10</i>
</address>
<div style="border-style: solid; border-color: rgb(38, 108, 111); border-width: 1px 2.5px 2px 0.5px; padding: 1pt 3pt; position: absolute; display: none; z-index: 1000; background-color: rgb(236, 231, 197); font-size: 10pt; font-family: sans-serif; color: rgb(1, 1, 34);"></div></body></html>